<!DOCTYPE html>
<html lang="zh-CN">

<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>P标签与Textarea外边距差异分析</title>
    <style>
        * {
            margin: 0;
            padding: 0;
            box-sizing: border-box;
            font-family: 'Segoe UI', Tahoma, Geneva, Verdana, sans-serif;
        }

        body {
            background: linear-gradient(135deg, #f5f7fa 0%, #c3cfe2 100%);
            color: #333;
            line-height: 1.6;
            padding: 20px;
            min-height: 100vh;
        }

        .container {
            max-width: 1000px;
            margin: 0 auto;
            padding: 20px;
        }

        header {
            text-align: center;
            margin-bottom: 40px;
            padding: 30px;
            background: white;
            border-radius: 10px;
            box-shadow: 0 5px 15px rgba(0, 0, 0, 0.1);
        }

        h1 {
            color: #2c3e50;
            margin-bottom: 15px;
        }

        .description {
            color: #7f8c8d;
            max-width: 800px;
            margin: 0 auto;
        }

        .demo-section {
            display: flex;
            flex-wrap: wrap;
            gap: 30px;
            margin-bottom: 40px;
        }

        .demo-box {
            flex: 1;
            min-width: 300px;
            background: white;
            padding: 25px;
            border-radius: 10px;
            box-shadow: 0 5px 15px rgba(0, 0, 0, 0.1);
        }

        .demo-title {
            color: #2c3e50;
            margin-bottom: 15px;
            padding-bottom: 10px;
            border-bottom: 2px solid #eee;
        }

        .code-block {
            background: #2d3436;
            color: #f5f6fa;
            padding: 20px;
            border-radius: 8px;
            overflow-x: auto;
            margin: 20px 0;
            font-family: 'Consolas', monospace;
            line-height: 1.5;
        }

        .comment {
            color: #dfe6e9;
            opacity: 0.8;
        }

        .explanation {
            margin-top: 20px;
            padding: 20px;
            background: #f8f9fa;
            border-left: 4px solid #3498db;
            border-radius: 4px;
        }

        .key-point {
            color: #2c3e50;
            font-weight: bold;
            margin: 10px 0;
        }

        .visual-guide {
            display: flex;
            justify-content: space-between;
            margin: 20px 0;
            padding: 10px;
            background: #f1f2f6;
            border-radius: 8px;
        }

        .margin-box {
            width: 8px;
            height: 60px;
            background: #e74c3c;
            border-radius: 2px;
            display: flex;
            justify-content: center;
            align-items: center;
            color: white;
            font-weight: bold;
            font-size: 12px;
        }

        .content-box {
            flex-grow: 1;
            height: 60px;
            background: #3498db;
            border-radius: 4px;
            display: flex;
            justify-content: center;
            align-items: center;
            color: white;
            font-weight: bold;
        }

        .footer {
            text-align: center;
            margin-top: 40px;
            color: #7f8c8d;
            font-size: 0.9em;
        }

        /* 原始代码样式 */
        .original-p {
            width: 100%;
            height: 80px;
            border: 1px solid purple;
            background-color: #3498db;
            color: white;
            padding: 10px;
            margin-bottom: 20px;
        }

        .original-textarea {
            width: 100%;
            height: 80px;
            border: 1px solid purple;
            background-color: #3498db;
            color: white;
            padding: 10px;
            resize: none;
        }

        .comparison-table {
            width: 100%;
            border-collapse: collapse;
            margin: 20px 0;
        }

        .comparison-table th,
        .comparison-table td {
            border: 1px solid #ddd;
            padding: 12px;
            text-align: left;
        }

        .comparison-table th {
            background-color: #f2f2f2;
        }

        .comparison-table tr:nth-child(even) {
            background-color: #f9f9f9;
        }

        @media (max-width: 768px) {
            .demo-section {
                flex-direction: column;
            }
        }
    </style>
</head>

<body>
    <div class="container">
        <header>
            <h1>P标签与Textarea外边距差异分析</h1>
            <p class="description">解析为什么在相同样式下，p标签和textarea元素表现不同</p>
        </header>

        <div class="demo-section">
            <div class="demo-box">
                <h2 class="demo-title">原始代码效果</h2>
                <p>这是您提供的代码，注意p标签和textarea的表现差异：</p>

                <div class="code-block">
                    <span class="comment">&lt;!-- 这是您的代码 --&gt;</span><br>
                    p {<br>
                    &nbsp;&nbsp;&nbsp;&nbsp;width: 100%;<br>
                    &nbsp;&nbsp;&nbsp;&nbsp;height: 80px;<br>
                    &nbsp;&nbsp;&nbsp;&nbsp;border: 1px solid purple;<br>
                    &nbsp;&nbsp;&nbsp;&nbsp;background-color: #3498db;<br>
                    }<br>
                    <br>
                    textarea {<br>
                    &nbsp;&nbsp;&nbsp;&nbsp;width: 100%;<br>
                    &nbsp;&nbsp;&nbsp;&nbsp;height: 80px;<br>
                    &nbsp;&nbsp;&nbsp;&nbsp;border: 1px solid purple;<br>
                    &nbsp;&nbsp;&nbsp;&nbsp;background-color: #3498db;<br>
                    }
                </div>

                <div>
                    <p class="original-p">两侧外边距是对称的</p>
                    <textarea class="original-textarea" placeholder="两侧外边距是不对称的"></textarea>
                </div>

                <div class="explanation">
                    <p class="key-point">为什么表现不同？</p>
                    <p>虽然p标签和textarea设置了相同的样式，但它们表现不同的原因：</p>
                    <ol>
                        <li><strong>默认display属性不同</strong> - p标签是block元素，textarea是inline-block元素</li>
                        <li><strong>默认盒模型行为不同</strong> - 不同元素的默认盒模型处理方式有细微差异</li>
                        <li><strong>浏览器默认样式差异</strong> - 浏览器对表单元素和非表单元素的默认样式处理不同</li>
                    </ol>
                </div>
            </div>

            <div class="demo-box">
                <h2 class="demo-title">详细解析</h2>
                <p>元素特性对比：</p>

                <table class="comparison-table">
                    <tr>
                        <th>特性</th>
                        <th>P标签</th>
                        <th>Textarea</th>
                    </tr>
                    <tr>
                        <td>默认display属性</td>
                        <td>block</td>
                        <td>inline-block</td>
                    </tr>
                    <tr>
                        <td>默认盒模型</td>
                        <td>content-box</td>
                        <td>border-box (大多数浏览器)</td>
                    </tr>
                    <tr>
                        <td>默认外边距</td>
                        <td>有上下外边距(1em)</td>
                        <td>无外边距</td>
                    </tr>
                    <tr>
                        <td>默认内边距</td>
                        <td>无</td>
                        <td>有(2px)</td>
                    </tr>
                    <tr>
                        <td>宽度计算</td>
                        <td>包含边框和内边距</td>
                        <td>不包含边框和内边距</td>
                    </tr>
                </table>

                <div class="explanation">
                    <p class="key-point">根本原因：默认盒模型差异</p>
                    <p>虽然您设置了相同的样式，但浏览器对这两种元素的默认处理方式不同：</p>
                    <ul>
                        <li>p标签默认使用<code>content-box</code>盒模型</li>
                        <li>textarea默认使用<code>border-box</code>盒模型（大多数现代浏览器）</li>
                        <li>这意味着相同的<code>width: 100%</code>在两个元素上计算方式不同</li>
                    </ul>
                </div>
            </div>
        </div>

        <div class="demo-box">
            <h2 class="demo-title">解决方案</h2>
            <p>要使两者表现一致，可以添加以下CSS：</p>

            <div class="code-block">
                <span class="comment">/* 统一盒模型 */</span><br>
                p, textarea {<br>
                &nbsp;&nbsp;&nbsp;&nbsp;box-sizing: border-box;<br>
                }<br>
                <br>
                <span class="comment">/* 重置默认样式 */</span><br>
                p {<br>
                &nbsp;&nbsp;&nbsp;&nbsp;margin: 0;<br>
                &nbsp;&nbsp;&nbsp;&nbsp;padding: 0;<br>
                }<br>
                <br>
                textarea {<br>
                &nbsp;&nbsp;&nbsp;&nbsp;margin: 0;<br>
                }<br>
                <br>
                <span class="comment">/* 然后应用您的样式 */</span><br>
                p, textarea {<br>
                &nbsp;&nbsp;&nbsp;&nbsp;width: 100%;<br>
                &nbsp;&nbsp;&nbsp;&nbsp;height: 80px;<br>
                &nbsp;&nbsp;&nbsp;&nbsp;border: 1px solid purple;<br>
                &nbsp;&nbsp;&nbsp;&nbsp;background-color: #3498db;<br>
                &nbsp;&nbsp;&nbsp;&nbsp;padding: 10px; <span class="comment">/* 显式设置padding */</span><br>
                }
            </div>

            <div>
                <p
                    style="box-sizing: border-box; margin: 0; padding: 10px; width: 100%; height: 80px; border: 1px solid purple; background-color: #3498db; color: white;">
                    两侧外边距现在对称了</p>
                <textarea
                    style="box-sizing: border-box; margin: 0; padding: 10px; width: 100%; height: 80px; border: 1px solid purple; background-color: #3498db; color: white; resize: none;"
                    placeholder="两侧外边距现在对称了"></textarea>
            </div>

            <div class="explanation">
                <p>通过统一盒模型和重置默认样式，p标签和textarea现在表现一致了。</p>
            </div>
        </div>

        <div class="footer">
            <p>P标签与Textarea外边距差异分析 &copy; 2023</p>
        </div>
    </div>
</body>

</html>